﻿using MediatR;
using SystemHYS.Domian.DTO;
using SystemHYS.Domian;
using SystemHYS.API.Application.command.GualityProjectCommand;
using SystemHYS.Infrstructre.Interface;

namespace SystemHYS.API.Application.commandler.GualityProjectCommandler
{
    public class GualityProjectShowCommandler:IRequestHandler<GualityProjectShowCommand, PageDTO<GualityProject>>
    {
        IGualityProjectRepository GroupRepository;
        public GualityProjectShowCommandler(IGualityProjectRepository GroupRepository)
        {
            this.GroupRepository = GroupRepository;
        }

        public async Task<PageDTO<GualityProject>> Handle(GualityProjectShowCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await GroupRepository.GetAllAsnc() where a.IsDel == false select a;
            if (request.AddTime != null)
            {
                list = list.Where(x => x.AddTime == request.AddTime);
            }
            if (request.gualityProjectNumbering != null) 
            {
                list = list.Where(x => x.gualityProjectNumbering.Contains(request.gualityProjectNumbering));
            }
            PageDTO<GualityProject> page = new PageDTO<GualityProject>();
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pagesize);
            page.plist = list.OrderByDescending(x => x.AddTime).Skip((request.pageindex - 1) / request.pagesize).Take(request.pagesize).ToList();
            return page;
        }
    }
}
